/******************************************************************************
 * Author:      671643387
 * Created:     2016/9/27
 *****************************************************************************/

var mErr = require('./error.js');

// 创建房间，由游戏服务器发起
exports.createGame = function(query, resp, callback) {
    var uid = +query.uid;
    var round = +query.args.round;

    gMahjong.createGame(uid, round, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

// 加入房间，玩家发起
exports.joinRoom = function(query, resp, callback){
    var conn = query.conn;
    var uid = +query.uid;
    var roomId = query.args.roomId;
    gMahjong.joinRoom(conn, roomId, uid, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

// 解散房间
exports.destroyGame = function(query, resp, callback){
    var conn = query.conn;
    var uid = +query.uid;
    var roomId = query.args.roomId;
    gMahjong.destroyGame(conn, roomId, uid, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};
// 报听，玩家发起
exports.baoTing = function(query, resp, callback){
    var uid = +query.uid;
    var roomId = query.args.roomId;
    var card = +query.args.card;
    gMahjong.baoTing(roomId, uid, card, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};


// 出牌，玩家发起
exports.playCard = function(query, resp, callback){
    var uid = +query.uid;
    var roomId = query.args.roomId;
    var card = +query.args.card;
    gMahjong.playCard(roomId, uid, card, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

// 杠牌，玩家发起
exports.gangCard = function(query, resp, callback){
    var uid = +query.uid;
    var roomId = query.args.roomId;
    gMahjong.gangCard(roomId, uid, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

// 碰牌，玩家发起
exports.pengCard = function(query, resp, callback){
    var uid = +query.uid;
    var roomId = query.args.roomId;
    gMahjong.pengCard(roomId, uid, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

// 胡牌，玩家发起
exports.huCard = function (query, resp, callback) {
    var uid = +query.uid;
    var roomId = query.args.roomId;
    gMahjong.huCard(roomId, uid, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

// 过，玩家发起
exports.pass = function(query, resp, callback){
    var uid = +query.uid;
    var roomId = query.args.roomId;
    gMahjong.pass(roomId, uid, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

// 继续
exports.continue = function(query, resp, callback){
    var uid = +query.uid;
    var roomId = query.args.roomId;
    gMahjong.continue(roomId, uid, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

// 发送提示消息
exports.sendTipMsg = function(query, resp, callback){
    var uid = +query.uid;
    var roomId = query.args.roomId;
    var msgId = query.args.msgId;
    var msg = query.args.msg;
    gMahjong.sendTipMsg(roomId, uid, msgId, msg, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

// 请求解散房间
exports.reqDestroyRoom = function(query, resp, callback){
    var uid = +query.uid;
    var roomId = query.args.roomId;
    gMahjong.reqDestroyRoom(roomId, uid, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

exports.respDestroyRoom = function(query, resp, callback){
    var uid = +query.uid;
    var roomId = query.args.roomId;
    var ok = query.args.ok;
    gMahjong.respDestroyRoom(roomId, uid, ok, function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};

exports.status = function(query, resp, callback){
    gMahjong.status(function(err, data){
        if (err) {
            resp.code = err;
            resp.desc = mErr.findString(err);
        } else {
            resp.data = data;
        }
        callback && callback();
    });
};